package com.dietdroid;

import java.util.ArrayList;

import android.content.Context;

public class DietDataOperations {
	
	public static ArrayList<NutritionHistoryModel> getDietHistory(int month,
			int day, int year, Context ctx) {
		
		ArrayList<NutritionHistoryModel> nutrition_history_list = new ArrayList<NutritionHistoryModel>();
		
		HistoryData history_data = DataStorage.getHistoryData(ctx);
		ArrayList<HistoryFoodItemModel> food_history_list = history_data.getHistoryForDate(month, year, day);
		
		
		if(food_history_list == null) {
			food_history_list = new ArrayList<HistoryFoodItemModel>();
		}
		
		int calories = 0;
		
		int total_fat = 0;
		
		int cholesterol = 0;
		
		int sodium = 0;
		
		int total_carbs = 0;
		
		int protein = 0;
		
		int sugars = 0;
		
		for(HistoryFoodItemModel model : food_history_list) {
			calories += model.getItem().getCalories() * model.getServings();
			total_fat += model.getItem().getTotal_fat() * model.getServings();
			cholesterol += model.getItem().getCholesterol() * model.getServings();
			sodium += model.getItem().getSodium() * model.getServings();
			total_carbs += model.getItem().getTotal_carbs() * model.getServings();
			protein += model.getItem().getProtein() * model.getServings();
			sugars += model.getItem().getSugars() * model.getServings();
		}
		
		ProfileData profile = DataStorage.getProfileData(ctx);
		
		NutritionHistoryModel caloriesModel = new NutritionHistoryModel("Calories", calories, profile.getMaxCals(), String.valueOf(calories));
		nutrition_history_list.add(caloriesModel);
		NutritionHistoryModel totalFatModel = new NutritionHistoryModel("Total Fat", total_fat, profile.getMaxFat(), String.valueOf(total_fat) + " g");
		nutrition_history_list.add(totalFatModel);
		NutritionHistoryModel cholesterolModel = new NutritionHistoryModel("Cholesterol", cholesterol, profile.getMaxChol(), String.valueOf(cholesterol) + " mg");
		nutrition_history_list.add(cholesterolModel);
		NutritionHistoryModel sodiumModel = new NutritionHistoryModel("Sodium", sodium, profile.getMaxSodium(), String.valueOf(sodium) + " mg");
		nutrition_history_list.add(sodiumModel);
		NutritionHistoryModel totalCarbsModel = new NutritionHistoryModel("Total Carbs", total_carbs, profile.getMaxCarbs(), String.valueOf(total_carbs) + " g");
		nutrition_history_list.add(totalCarbsModel);
		NutritionHistoryModel proteinModel = new NutritionHistoryModel("Protein", protein, profile.getMaxProtein(), String.valueOf(protein) + " g");
		nutrition_history_list.add(proteinModel);
		NutritionHistoryModel sugarsModel = new NutritionHistoryModel("Sugars", sugars, profile.getMaxSugar(), String.valueOf(sugars) + " g");
		nutrition_history_list.add(sugarsModel);
		
		
		return nutrition_history_list;
				
		
	}

	public static ArrayList<String> getConsumedFoods(
			int month, int day, int year, Context ctx) {
		ArrayList<String> foods = new ArrayList<String>();
		HistoryData history_data = DataStorage.getHistoryData(ctx);
		ArrayList<HistoryFoodItemModel> food_history_list = history_data.getHistoryForDate(month, year, day);
		if(food_history_list == null) {
			return null;
		}
		for(HistoryFoodItemModel model : food_history_list) {
			if(!foods.contains(model.getItem().getFood_name())) {
				foods.add(model.getItem().getFood_name());
			}
		}
		return foods;
	}

	public static ArrayList<NutritionFoodHistoryModel> getNutritionFactFoods(
			int month, int day, int year, String nutrition_fact, Context ctx) {
		
		ArrayList<NutritionFoodHistoryModel> nutrition_history_list = new ArrayList<NutritionFoodHistoryModel>();
		
		HistoryData history_data = DataStorage.getHistoryData(ctx);
		ArrayList<HistoryFoodItemModel> food_history_list = history_data.getHistoryForDate(month, year, day);
		
		if(food_history_list == null) {
			return null;
		}
		int calories = 0;
		
		int total_fat = 0;
		
		int cholesterol = 0;
		
		int sodium = 0;
		
		int total_carbs = 0;
		
		int protein = 0;
		
		int sugars = 0;
		
		for(HistoryFoodItemModel model : food_history_list) {
			calories += model.getItem().getCalories() * model.getServings();
			total_fat += model.getItem().getTotal_fat() * model.getServings();
			cholesterol += model.getItem().getCholesterol() * model.getServings();
			sodium += model.getItem().getSodium() * model.getServings();
			total_carbs += model.getItem().getTotal_carbs() * model.getServings();
			protein += model.getItem().getProtein() * model.getServings();
			sugars += model.getItem().getSugars() * model.getServings();
		}
		
		if(nutrition_fact.equals("Calories")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getCalories() * model.getServings()) * 100)/calories);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}
		}else if(nutrition_fact.equals("Total Fat")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getTotal_fat() * model.getServings()) * 100)/total_fat);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}
		}else if(nutrition_fact.equals("Cholesterol")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getCholesterol() * model.getServings()) * 100)/cholesterol);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}
		}else if(nutrition_fact.equals("Sodium")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getSodium() * model.getServings()) * 100)/sodium);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}
		}else if(nutrition_fact.equals("Total Carbs")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getTotal_carbs() * model.getServings()) * 100)/total_carbs);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}
		}else if(nutrition_fact.equals("Protein")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getProtein() * model.getServings()) * 100)/protein);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}			
		}else if(nutrition_fact.equals("Sugars")) {
			for(HistoryFoodItemModel model : food_history_list) {
				long percent = Math.round(((model.getItem().getSugars() * model.getServings()) * 100)/sugars);
				nutrition_history_list.add(new NutritionFoodHistoryModel(model.getItem().getFood_name(), percent));
			}				
		} else{
			//should never get here
		}
		
		return nutrition_history_list;
	}
	

}
